﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using PRET.ReportDataDefine.UIData;
using PRET.ReportControlLib.ServiceReference;

namespace PRET.ReportControlLib.ReportView
{
    public partial class HighLowSatusReportView : UserControl
    {
        private PRETWebServiceClient adapter = null;

        public DateTime GetSelectMonth
        {
            get
            {
                return this.dateTimePicker.Value;
            }
        }
        public String GetDeviceID
        {
            get 
            {
                string[] deviceID = this.deviceComboBox.Text.Split(new char[] { ' ' });
                if (deviceID.Length > 0 && deviceID[0].Length > 0)
                    return deviceID[0];
                else
                    return this.deviceComboBox.Text; 
            }
        }

        public HighLowSatusReportView()
        {
            InitializeComponent();
            this.dateTimePicker.Value = DateTime.Today;
            adapter = new PRETWebServiceClient();
        }

        public void SetDeviceList()
        {
            if (this.adapter == null)
            {
                MessageBox.Show("HighLowSatusReportView 没有初始化Server Adapter!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string[] deviceList = this.adapter.GetHighLowDeviceList();
            if (deviceList != null && deviceList.Length > 0)
                this.deviceComboBox.DataSource = deviceList.ToList();
        }

        private void reportViewBtn_Click(object sender, EventArgs e)
        {
            if (this.adapter == null)
            {
                MessageBox.Show("HighLowSatusReportView 没有初始化Server Adapter!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (this.CorrectData() == false)
            {
                MessageBox.Show("请选择正确的设备名和月份!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.highLowStatusReportViewer.LocalReport.DataSources.Clear(); //clear report
            // 从后台获取高低柜状态列表
            HighLowStatusReport[] list = this.adapter.GetHighLowStatusReport(GetDeviceID, GetSelectMonth);
            if (list != null && list.Length > 0)
            {
                Microsoft.Reporting.WinForms.ReportDataSource dataset = new Microsoft.Reporting.WinForms.ReportDataSource("HighLowStatusReportDS"); // set the datasource
                this.highLowStatusReportViewer.LocalReport.DataSources.Add(dataset);
                dataset.Value = list.ToList<HighLowStatusReport>();
            }
            this.highLowStatusReportViewer.LocalReport.Refresh();
            this.highLowStatusReportViewer.RefreshReport();
        }

        private bool CorrectData()
        {
            string deviceID = this.GetDeviceID;
            if (string.IsNullOrEmpty(deviceID))
                return false;
            return true;
        }
    }
}
